package com.ruoyi.employee.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.employee.domain.Departments;
import com.ruoyi.employee.service.IDepartmentsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 部门Controller
 * 
 * @author ruoyi
 * @date 2025-03-01
 */
@RestController
@RequestMapping("/departments")
public class DepartmentsController extends BaseController
{
    @Autowired
    private IDepartmentsService departmentsService;

    /**
     * 查询部门列表
     */
    @RequiresPermissions("employee:departments:list")
    @GetMapping("/list")
    public TableDataInfo list(Departments departments)
    {
        startPage();
        List<Departments> list = departmentsService.selectDepartmentsList(departments);
        return getDataTable(list);
    }
    /**
     * 查询部门
     */
    @RequiresPermissions("employee:departments:list")
    @GetMapping("/option")
    public List<Departments> list()
    {
        return departmentsService.selectDepartments();
    }
    /**
     * 导出部门列表
     */
    @RequiresPermissions("employee:departments:export")
    @Log(title = "部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Departments departments)
    {
        List<Departments> list = departmentsService.selectDepartmentsList(departments);
        ExcelUtil<Departments> util = new ExcelUtil<Departments>(Departments.class);
        util.exportExcel(response, list, "部门数据");
    }

    /**
     * 获取部门详细信息
     */
    @RequiresPermissions("employee:departments:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(departmentsService.selectDepartmentsById(id));
    }

    /**
     * 新增部门
     */
    @RequiresPermissions("employee:departments:add")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Departments departments)
    {
        return toAjax(departmentsService.insertDepartments(departments));
    }

    /**
     * 修改部门
     */
    @RequiresPermissions("employee:departments:edit")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Departments departments)
    {
        return toAjax(departmentsService.updateDepartments(departments));
    }

    /**
     * 删除部门
     */
    @RequiresPermissions("employee:departments:remove")
    @Log(title = "部门", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(departmentsService.deleteDepartmentsByIds(ids));
    }
}
